var api = new function(){
var SELF = this;
this.apiUrl = location.origin + '/api/';
this.http = function(path,data,CB,isJson,opt){
	isJson = isJson || false 
	opt = opt || {}
	mui.ajax(SELF.apiUrl+path,{
		data:data,
		type:opt.type || 'post',
		cache:false,
		timeout:10000,
		dataType:'json',
		success:function(data){
			if(CB)CB(data);
		},
		headers: isJson?{'Content-Type':'application/json'}:{},
		error:function(xhr,type,errorThrown){
			console.error('[ERROR]'+JSON.stringify([path,type,errorThrown]));
		}
	})
}
// 分页运算器
this.PAGE_START = 1
this.PAGE_MAP = {}
this.pageIndexer = function(key){
	!this.PAGE_MAP[key]?this.PAGE_MAP[key]={i:SELF.PAGE_START,list:0}:null;
	return this.PAGE_MAP[key].i;
}
this.pageClear = function(key){
	!this.PAGE_MAP[key]?this.PAGE_MAP[key]={i:SELF.PAGE_START,list:0}:null;
	this.PAGE_MAP[key] = {i:SELF.PAGE_START,list:0}
}
this.pageLoader = function(key, list, size){
	!this.PAGE_MAP[key]?this.PAGE_MAP[key]={i:0,list:0}:null;
	size = size || 10
	var totalNum = this.PAGE_MAP[key].i * size
	if(this.PAGE_START != 0)totalNum = totalNum - size;
	var lastNum = list.length - (this.PAGE_MAP[key].list - totalNum)
	var subList = []
	for(var i=0; i<lastNum; i++){
		subList.push(list[list.length-(lastNum-i)])
	}
	this.PAGE_MAP[key].list = this.PAGE_MAP[key].list + lastNum
	if(list.length == size){
		this.PAGE_MAP[key].i++;		
	}
	return subList;
}


//奇葩编码转图片
this.toImgPath = function(html){
	var el = document.createElement('div');
	el.innerHTML = html;	el.innerHTML = el.innerText;
	var imgs = el.getElementsByTagName('img');	
	if(imgs.length!=0){
		return imgs;
	}
	var eee = document.createElement("div");
	eee.innerHTML = html
	var imgs = eee.getElementsByTagName('img');	
	return imgs;
}
this.unescapeHTML = function(a){
    a = "" + a;
    return a.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(/&apos;/g, "'");
}

}
